package com.google.shop.product.service.impl.card;

import com.google.shop.product.entity.card.Card;
import com.google.shop.product.repository.card.CardRepository;
import com.google.shop.product.service.card.CardService;
import com.google.shop.product.vo.card.CardListVO;
import com.google.shop.product.vo.card.CardVO;
import com.google.shop.product.vo.card.carddetailvo.CardDetailSkuVO;
import com.google.shop.product.vo.card.carddetailvo.CardDetailVO;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
/* loaded from: app.jar:BOOT-INF/classes/com/google/shop/product/service/impl/card/CardImpl.class */
public class CardImpl implements CardService {
    @Autowired
    CardRepository cardRepository;

    @Override // com.google.shop.product.service.card.CardService
    public CardListVO list(String buyerId) {
        List<Card> cardList = this.cardRepository.findListByBuyerId(Integer.valueOf(buyerId));
        CardListVO cardListVO = new CardListVO();
        List<CardVO> cardVOList = new ArrayList<>();
        for (Card card : cardList) {
            CardVO cardVO = new CardVO();
            BeanUtils.copyProperties(card, cardVO);
            cardVO.setCardSkuPrice(card.getCardSkuPrice() + "");
            cardVO.setCardSkuTag(card.getCardSkuTag().split(BeanFactory.FACTORY_BEAN_PREFIX));
            cardVOList.add(cardVO);
        }
        cardListVO.setCardVOList(cardVOList);
        return cardListVO;
    }

    @Override // com.google.shop.product.service.card.CardService
    public void updateCardIsOpt(String buyerId, Integer skuId, Integer isOpt) {
        List<Card> lineCard = (List) this.cardRepository.findListByBuyerId(Integer.valueOf(buyerId)).stream().filter(s -> {
            return s.getCardSkuId() == skuId;
        }).collect(Collectors.toList());
        lineCard.get(0).setCardSkuIsopt(isOpt);
        this.cardRepository.save(lineCard.get(0));
    }

    @Override // com.google.shop.product.service.card.CardService
    public void updateCardSkuNum(String buyerId, Integer skuId, Integer skuNum) {
        List<Card> lineCard = (List) this.cardRepository.findListByBuyerId(Integer.valueOf(buyerId)).stream().filter(s -> {
            return s.getCardSkuId() == skuId;
        }).collect(Collectors.toList());
        lineCard.get(0).setCardSkuNum(skuNum);
        if (skuNum != null) {
            lineCard.get(0).setCardSkuAmount(Integer.valueOf((int) (((lineCard.get(0).getCardSkuPrice().doubleValue() * ((double) skuNum.intValue())) + ((double) lineCard.get(0).getCardSkuFreight().intValue())) * 100.0d)));
        }
        this.cardRepository.save(lineCard.get(0));
    }

    @Override // com.google.shop.product.service.card.CardService
    public void updateAllCardOpt(String buyerId, Boolean isAllOpt) {
        List<Card> cardList = this.cardRepository.findListByBuyerId(Integer.valueOf(buyerId));
        if (isAllOpt.booleanValue()) {
            for (Card card : cardList) {
                card.setCardSkuIsopt(0);
            }
        } else {
            for (Card card2 : cardList) {
                card2.setCardSkuIsopt(1);
            }
        }
        this.cardRepository.saveAll((Iterable) cardList);
    }

    @Override // com.google.shop.product.service.card.CardService
    public void saveItemGoCard(String buyerId, CardVO cardVO) {
        if (this.cardRepository.findCardByCardSkuIdAndBuyerId(cardVO.getCardSkuId(), Integer.valueOf(buyerId)) == null) {
            Card card = new Card();
            BeanUtils.copyProperties(cardVO, card);
            card.setCardSkuTag(StringUtils.join(cardVO.getCardSkuTag(), BeanFactory.FACTORY_BEAN_PREFIX));
            card.setCardSkuPrice(BigDecimal.valueOf(Double.valueOf(cardVO.getCardSkuPrice()).doubleValue() / 100.0d));
            card.setBuyerId(Integer.valueOf(buyerId));
            this.cardRepository.save(card);
        }
    }

    @Override // com.google.shop.product.service.card.CardService
    public void deleteCard(String buyerId) {
        System.out.println(this.cardRepository.deleteByBuyerIdAndCardSkuIsopt(Integer.valueOf(buyerId), 0));
    }

    @Override // com.google.shop.product.service.card.CardService
    public CardDetailVO findCardDetailByBuyerId(String buyerId) {
        List<Card> cardList = this.cardRepository.findCardsByBuyerIdAndAndCardSkuIsopt(Integer.valueOf(buyerId), 0);
        List<CardDetailSkuVO> detailSkuVOList = new ArrayList<>();
        CardDetailVO cardDetailVO = new CardDetailVO();
        for (Card card : cardList) {
            CardDetailSkuVO detailSkuVO = new CardDetailSkuVO();
            BeanUtils.copyProperties(card, detailSkuVO);
            detailSkuVO.setCardSkuTag(card.getCardSkuTag().split(BeanFactory.FACTORY_BEAN_PREFIX));
            detailSkuVO.setCardSkuPrice(card.getCardSkuPrice().toString());
            detailSkuVO.setCardSkuAmount(BigDecimal.valueOf(Double.valueOf((double) card.getCardSkuAmount().intValue()).doubleValue() / 100.0d));
            detailSkuVOList.add(detailSkuVO);
        }
        cardDetailVO.setCardDetailSkuVOList(detailSkuVOList);
        return cardDetailVO;
    }
}
